package com.mtnz.njk.Service;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.*;
import com.mtnz.njk.Util.DateUtil;
import com.mtnz.njk.entity.*;
import com.mtnz.njk.model.*;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
......................我佛慈悲......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG...................
 */
@Service
public class CommunityService {

    @Autowired
    PostSpecialMapper postSpecialMapper;  //帖子专题

    @Autowired
    PostMapper postMapper;//帖子

    @Autowired
    PraiseAskMapper praiseAskMapper;//点赞

    @Autowired
    CommunityUserMapper communityUserMapper; //用户信息

    @Autowired
    CommunityUserInfoMapper communityUserInfoMapper; //用户扩展信息

    @Autowired
    AskMapper askMapper;//提问

    @Autowired
    AskImgMapper askImgMapper;//提问图

    @Autowired
    CollectionAskMapper collectionAskMapper;

    @Autowired
    CommentAskMapper commentAskMapper;//提问回复

    @Autowired
    CommentAskImgMapper commentAskImgMapper;//回复提问图

    @Autowired
    ResearchMapper researchMapper;//研究院

    @Autowired
    ResearchImgMapper researchImgMapper;//研究院图

    @Autowired
    PraiseResearchMapper praiseResearchMapper;//研究院点赞

    @Autowired
    PostKeyWordMapper postKeyWordMapper;//帖子关键字查询

    @Autowired
    PraisePostMapper praisePostMapper;//帖子点赞

    @Autowired
    PostImgMapper postImgMapper;

    @Autowired
    PostBannerMapper postBannerMapper;

    @Autowired
    UserTaskMapperr userTaskMapperr;

    @Autowired
    HistoryMapper historyMapper;//我看过的历史

    @Autowired
    NewsMapper newsMapper;

    /**
     * 社区首页
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    // @Cacheable(cacheNames = "findCommunity")
    public ResponseResult findCommunity(String userId, String status,Integer pageNum, Integer pageSize) throws Exception {
        //混查所有的提问和研究院帖子
        PageHelper.startPage(pageNum, pageSize);
        List<PostAllModel> postAll = postMapper.findPostAll("tor",status);
        PageInfo pageInfo = new PageInfo<>(postAll);

        for (PostAllModel postAllModel : postAll) {
            //获取用户信息
            NjkUserEntity njkUserEntity = communityUserMapper.selectByPrimaryKey(postAllModel.getUserId());
            //获取用户扩展信息
            Example example3 = new Example(NjkUserInfoEntity.class);
            example3.createCriteria().andEqualTo("userId", postAllModel.getUserId());
            List<NjkUserInfoEntity> njkUserInfoEntities = communityUserInfoMapper.selectByExample(example3);
            for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                //判断是否是提问
                if ("t".equals(postAllModel.getTor())) {
                    PraiseAskEntity praiseAskEntity = praiseAskMapper.findById(postAllModel.getId(), userId);
                    if (praiseAskEntity != null) {
                        postAllModel.setZan("1");
                    } else {
                        postAllModel.setZan("0");
                    }
                    CollectionAskEntity collection = collectionAskMapper.findCollection(userId, postAllModel.getId());
                    if (collection != null) {
                        postAllModel.setCollection("1");
                    } else {
                        postAllModel.setCollection("0");
                    }
                    AskEntity askEntity = askMapper.shareViews(postAllModel.getId());

                    if (askEntity!=null&&!"".equals(askEntity)) {
                        postAllModel.setShareViews(askEntity.getShareViews());
                    }
                    postAllModel.setAskImgEntityList(askImgMapper.findById(postAllModel.getId()));
                    postAllModel.setImg("");
                    postAllModel.setStatus("0");
                    postAllModel.setSpecialImg("");
                    postAllModel.setZanViews(praiseAskMapper.findCount(postAllModel.getId()));
                    postAllModel.setHuiFu(commentAskMapper.findCount("0", postAllModel.getId()));
                    postAllModel.setUserName(njkUserEntity.getNickName());
                    postAllModel.setUserImg(njkUserInfoEntity.getImg());
                    postAllModel.setCommentAskModels(commentAskMapper.findListReply("0", postAllModel.getId()));
                    postAllModel.setUrl("http://www.meitiannongzi.com/nongjike/static/jsp/share_tiwen.html?TIWEN_ID=" + postAllModel.getId() + "&uid=" + userId);
                    postAllModel.setIntro("");
                    postAllModel.setProductName("");
                }  else if ("r".equals(postAllModel.getTor())) {
                    ResearchEntity researchEntity = researchMapper.showListResearch(postAllModel.getId());
                    if (praiseResearchMapper.findById(postAllModel.getId(), userId) != null) {
                        postAllModel.setZan("1");
                    } else {
                        postAllModel.setZan("0");
                    }
                    List<ResearchImgEntity> listImg = new ArrayList();
                    if (!researchEntity.getActiveStatus().equals("2")&&researchEntity.getActiveStatus()!=null) {
                        listImg = getResearchIntro(postAllModel, researchEntity);
                    } else {
                        listImg = getResearchIntro(postAllModel, researchEntity);
                    }
                    postAllModel.setShareViews(researchEntity.getShareViews());
                    postAllModel.setResearchImg(researchImgMapper.showResearchImg(researchEntity.getResearchId().toString()));
                    postAllModel.setResearchActiveImg(listImg);
                    postAllModel.setImg("");
                    postAllModel.setUserName(njkUserEntity.getNickName());
                    postAllModel.setUserImg(njkUserInfoEntity.getImg());
                    postAllModel.setCollection(researchEntity.getProductId());
                    postAllModel.setProName(postAllModel.getProName() + "讨论区");
                    if (researchEntity.getActiveStatus().equals("2") || researchEntity.getActiveStatus().equals("1")) {
                        postAllModel.setUrl("https://www.meitiannongzi.com/nongjike/static/jsp/activity.html?researchId=" + researchEntity.getResearchId() + "&uid=" + userId);
                    } else {
                        postAllModel.setUrl("http://www.meitiannongzi.com/nongjike/static/jsp/Researc.html?researchId=" + researchEntity.getResearchId() + "&uid=" + userId);
                    }
                }
            }
        }

        return new ResponseResult(ResponseMessage.OK, pageInfo);
    }

    /**
     * 首页广告图和社区置顶内容
     *
     * @return
     * @throws Exception
     */
    public ResponseResult homeBanner() throws Exception {

        //首页广告
        Example example = new Example(PostBannerEntity.class);
        example.createCriteria().andEqualTo("status", "1");
        List<PostBannerEntity> postBannerEntities = postBannerMapper.selectByExample(example);
        PostBannerEntity postBannerEntity = postBannerEntities.get(0);

        //置顶内容
        //获取帖子专题topStatus等于1的数据
        Example example1 = new Example(PostSpecialEntity.class);
        example1.createCriteria().andNotEqualTo("specialId", "")
                .andNotEqualTo("specialId", null)
                .andNotEqualTo("topStatus", "")
                .andNotEqualTo("topStatus", null)
                .andEqualTo("topStatus", "1");
        List<PostSpecialEntity> askSpecialEntities = postSpecialMapper.selectByExample(example1);
        for (PostSpecialEntity askSpecialEntity : askSpecialEntities) {
            Example example2 = new Example(PostEntity.class);
            example2.createCriteria().andEqualTo("specialId", askSpecialEntity.getSpecialId());
            int i = postMapper.selectCountByExample(example2);
            askSpecialEntity.setTotal(i);
            postBannerEntity.setPostSpecialEntities(askSpecialEntity);
        }
        //获取帖子topStatus等于1的数据
        Example example3 = new Example(PostEntity.class);
        example3.createCriteria().andNotEqualTo("specialId", "")
                .andNotEqualTo("specialId", null)
                .andNotEqualTo("topStatus", "")
                .andNotEqualTo("topStatus", null)
                .andNotEqualTo("userId", "")
                .andNotEqualTo("userId", null)
                .andEqualTo("topStatus", "1")
                .andEqualTo("activityStatus", "0");
        List<PostEntity> askEntities = postMapper.selectByExample(example3);
        postBannerEntity.setPostEntities(askEntities);
        return new ResponseResult(ResponseMessage.OK, postBannerEntity);
    }

    /**
     * 帖子关键字查询
     */
    public ResponseResult SearchPost(String keyWord, Integer pageNum, Integer pageSize)throws Exception {
        Example example = new Example(PostKeyWordEntity.class);
        example.createCriteria().andLike("keyword","%"+ keyWord+"%");
        List<PostKeyWordEntity> postKeyWordEntities = postKeyWordMapper.selectByExample(example);
        PostKeyWordEntity postKeyWordEntity = postKeyWordEntities.get(0);
        //根据关键字获取内容
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<SearchPostModel> postSearch = postMapper.findPostSearch(keyWord);
        //判断输入不为空
        PostKeyWordEntity postKeyWordEntitys = new PostKeyWordEntity();
        if (keyWord != "" && keyWord != null) {
            if (postKeyWordEntity != null){
                //修改点击数
                postKeyWordEntity.setViews(postKeyWordEntity.getViews()+1);
                postKeyWordEntity.getPostKeywordId();
                postKeyWordEntity.getKeyword();
                postKeyWordMapper.updateViews(postKeyWordEntity);
            }else {
                //添加关键子
                postKeyWordEntitys.setPostKeywordId(IdUtil.simpleUUID());
                postKeyWordEntitys.setKeyword(keyWord);
                postKeyWordEntitys.setViews(0);
                postKeyWordMapper.insert(postKeyWordEntitys);
            }

            for (SearchPostModel search : postSearch) {
                search.setTIME(search.getCreateDate());
                String[] split = search.getYUE().split("-");
                search.setYUE(split[0]);
                search.setRI(split[1]);
                //点击量
                Example example1 = new Example(PraisePostEntity.class);
                example1.createCriteria().andEqualTo("askId",search.getTID());
                Integer views = praisePostMapper.selectCountByExample(example1);
                search.setVIEWS(views);
                //帖子图
                Example example2 = new Example(PostImgEntity.class);
                example2.createCriteria().andEqualTo("askId",search.getTID());
                List<PostImgEntity> postImgEntities = postImgMapper.selectByExample(example2);
                search.setPOST_IMG(postImgEntities);
            }
        }

        PageInfo pageInfo = new PageInfo<>(postSearch);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     * 社区关键字搜索前十的数据
     */
    public ResponseResult hotKeyword()throws Exception{
        List<PostKeyWordEntity> postKeyWordEntities = postKeyWordMapper.hotKeyword();
        return new ResponseResult(ResponseMessage.OK,postKeyWordEntities);
    }

    /**
     * 我的提问
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResponseResult findUserAsk(String userId, Integer pageNum,Integer pageSize)throws Exception{
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<UserAskModel> userAsk = askMapper.findUserAsk(userId);
        for (UserAskModel userAskModel : userAsk) {
            Example example = new Example(AskImgEntity.class);
            example.createCriteria().andEqualTo("askId",userAskModel.getAskId());
            List<AskImgEntity> askImgEntities = askImgMapper.selectByExample(example);
            userAskModel.setAskImg(askImgEntities);
            userAskModel.setUrl("http://m.nongjike.cn/NJK/static/jsp/tiwen.html?TIWEN_ID=" + userAskModel.getAskId());
        }
        PageInfo pageInfo = new PageInfo<>(userAsk);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }


    /**
     * 查询我的所有帖子和提问
     * @param userId 用户ID
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult findUserCommunity(String userId, Integer pageNum,Integer pageSize)throws Exception{
        //获取用户信息
        NjkUserEntity njkUserEntity = communityUserMapper.selectByPrimaryKey(userId);
        //获取用户扩展信息
        Example example = new Example(NjkUserInfoEntity.class);
        example.createCriteria().andEqualTo("userId",userId );
        List<NjkUserInfoEntity> njkUserInfoEntities = communityUserInfoMapper.selectByExample(example);
        NjkUserInfoEntity njkUserInfoEntity = njkUserInfoEntities.get(0);
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<PostAllModel> postUserAll = postMapper.findPostUserAll(userId);
        for (PostAllModel postAllModel : postUserAll) {
            if ("t".equals(postAllModel.getTor())){
                PraiseAskEntity byId = praiseAskMapper.findById(postAllModel.getId(), userId);
                if (byId != null) {
                    postAllModel.setZan("1");
                } else {
                    postAllModel.setZan("0");
                }
                CollectionAskEntity collection = collectionAskMapper.findCollection(userId, postAllModel.getId());
                if (collection != null) {
                    postAllModel.setCollection("1");
                } else {
                    postAllModel.setCollection("0");
                }
                postAllModel.setAskImgEntityList(askImgMapper.findById(postAllModel.getId()));
                postAllModel.setImg("");
                postAllModel.setStatus("0");
                postAllModel.setSpecialImg("");
                postAllModel.setZanViews(praiseAskMapper.findCount(postAllModel.getId()));
                postAllModel.setHuiFu(commentAskMapper.findCount("0", postAllModel.getId()));
                postAllModel.setUserName(njkUserEntity.getNickName());
                postAllModel.setUserImg(njkUserInfoEntity.getImg());
                postAllModel.setCommentAskModels(commentAskMapper.findListReply("0", postAllModel.getId()));
                postAllModel.setUrl("http://www.meitiannongzi.com/nongjike/static/jsp/share_tiwen.html?TIWEN_ID=" + postAllModel.getId() + "&uid=" + userId);
                postAllModel.setIntro("");
                postAllModel.setProductName("");
            }else if ("r".equals(postAllModel.getTor())){
                ResearchEntity researchEntity = researchMapper.showListResearch(postAllModel.getId());
                if (praiseResearchMapper.findById(postAllModel.getId(), userId) != null) {
                    postAllModel.setZan("1");
                } else {
                    postAllModel.setZan("0");
                }
                List<ResearchImgEntity> listImg = new ArrayList();
                if (!researchEntity.getActiveStatus().equals("2")) {
                    listImg = getResearchIntro(postAllModel, researchEntity);
                } else {
                    listImg = getResearchIntro(postAllModel, researchEntity);
                }
                postAllModel.setResearchImg(researchImgMapper.showResearchImg(researchEntity.getResearchId().toString()));
                postAllModel.setResearchActiveImg(listImg);
                postAllModel.setImg("");
                postAllModel.setUserName(njkUserEntity.getNickName());
                postAllModel.setUserImg(njkUserInfoEntity.getImg());
                postAllModel.setCollection(researchEntity.getProductId());
                postAllModel.setProName(postAllModel.getProName() + "讨论区");
                if (researchEntity.getActiveStatus().equals("2") || researchEntity.getActiveStatus().equals("1")) {
                    postAllModel.setUrl("https://www.meitiannongzi.com/nongjike/static/jsp/activity.html?researchId=" + researchEntity.getResearchId() + "&uid=" + userId);
                } else {
                    postAllModel.setUrl("http://www.meitiannongzi.com/nongjike/static/jsp/Researc.html?researchId=" + researchEntity.getResearchId() + "&uid=" + userId);
                }
        }

        }
        PageInfo pageInfo = new PageInfo<>(postUserAll);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     *我关注的
     */
    public ResponseResult findCollectionAsk(String userId, Integer pageNum,Integer pageSize)throws Exception{
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<UserCollectionModel> collectionAsk = collectionAskMapper.findCollectionAsk(userId);
        for (UserCollectionModel userCollectionModel : collectionAsk) {
            userCollectionModel.setUserId1(userId);
            PraiseAskEntity byId = praiseAskMapper.findById(userCollectionModel.getAskId(), userId);
            if (byId!=null){
                userCollectionModel.setZan("1");
            }else {
                userCollectionModel.setZan("0");
            }
            userCollectionModel.setUrl("http://m.nongjike.cn/NJK/static/jsp/tiwen.html?TIWEN_ID=" + userCollectionModel.getAskId());
            List<AskImgEntity> byId1 = askImgMapper.findById(userCollectionModel.getAskId());
            userCollectionModel.setAskImg(byId1);
            userCollectionModel.setCollectioon("1");
        }
        PageInfo pageInfo = new PageInfo<>(collectionAsk);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     * 删除我看过的历史
     */
    @Transactional
    public ResponseResult deleteHistory(String userId)throws Exception{
        //根据当前用户Id删除所有
        Integer integer = historyMapper.deleteAll(userId);
        Map map = new HashMap<>();
        if (integer != null){
            map.put("message","成功");
        }else {
            map.put("message","失败");
        }
        return new ResponseResult(ResponseMessage.OK,map);
    }

    /**
     * 我的消息
     */
    public ResponseResult findNews(String userId,Integer pageNum,Integer pageSize) throws Exception {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<NewsEntity> news = newsMapper.findNews(userId);

        for (NewsEntity newsEntity : news) {
            String createDate = newsEntity.getCreateDate();
            Long day = DateUtil.TimeDiff2(createDate, DateUtil.getTime());
            Long hour = DateUtil.TimeDiff1(createDate, DateUtil.getTime());
            Long fenzhong = DateUtil.TimeDiff(createDate, DateUtil.getTime());
            if (day == 0) {
                if (hour < 1) {
                    newsEntity.setCreateDate(fenzhong+"分钟前");
                } else {
                    newsEntity.setCreateDate(hour+"小时前");
                }
            }
        }
        NjkUserEntity njkUserEntity = communityUserMapper.selectByPrimaryKey(userId);
        njkUserEntity.setStatus("0");
        communityUserMapper.updateByPrimaryKeySelective(njkUserEntity);
        PageInfo pageInfo = new PageInfo<>(news);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     *我的消息活动
     */
    public ResponseResult findActivityPost(Integer pageNum,Integer pageSize)throws Exception{
        //分页
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(ResearchEntity.class);
        example.createCriteria().andEqualTo("activeStatus",1);
        List<ResearchEntity> researchEntities = researchMapper.selectByExample(example);
        for (ResearchEntity researchEntity : researchEntities) {
            researchEntity.setName(researchEntity.getName()+"发布了活动");
            Example example1 = new Example(ResearchImgEntity.class);
            example1.createCriteria().andEqualTo("researchId",researchEntity.getResearchId());
            List<ResearchImgEntity> list = researchImgMapper.selectByExample(example1);
            researchEntity.setResearchImgEntities(list);
            Example example2 = new Example(NjkUserInfoEntity.class);
            example2.createCriteria().andEqualTo("userId",researchEntity.getUserId());
            List<NjkUserInfoEntity> njkUserInfoEntities = communityUserInfoMapper.selectByExample(example2);
            for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                researchEntity.setUserImg(njkUserInfoEntity.getImg());
            }

        }
        PageInfo pageInfo = new PageInfo<>(researchEntities);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     * 我看过的历史
     */
    public ResponseResult findHistory(String userId, Integer pageNum,Integer pageSize){
        //分页
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(HistoryEntity.class);
        example.createCriteria().andEqualTo("userId",userId);
        List<HistoryEntity> historyEntities = historyMapper.selectByExample(example);
        for (HistoryEntity historyEntity : historyEntities) {
            PostEntity postEntity = postMapper.selectByPrimaryKey(historyEntity.getId());
            if (postEntity!=null){
                Example example1 = new Example(NjkUserEntity.class);
                example1.createCriteria().andEqualTo("userId",postEntity.getUserId());
                List<NjkUserEntity> njkUserEntities = communityUserMapper.selectByExample(example1);
                for (NjkUserEntity njkUserEntity : njkUserEntities) {
                    historyEntity.setUserName(njkUserEntity.getNickName());
                }
                Example example2 = new Example(NjkUserInfoEntity.class);
                example2.createCriteria().andEqualTo("userId",postEntity.getUserId());
                List<NjkUserInfoEntity> njkUserInfoEntities = communityUserInfoMapper.selectByExample(example2);
                for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                    historyEntity.setImg(njkUserInfoEntity.getImg());
                }
            }
            AskEntity askEntity = askMapper.selectByPrimaryKey(historyEntity.getId());
            if (askEntity!=null){
                Example example1 = new Example(NjkUserEntity.class);
                example1.createCriteria().andEqualTo("userId",askEntity.getUserId());
                List<NjkUserEntity> njkUserEntities = communityUserMapper.selectByExample(example1);
                for (NjkUserEntity njkUserEntity : njkUserEntities) {
                    historyEntity.setUserName(njkUserEntity.getNickName());
                }
                Example example2 = new Example(NjkUserInfoEntity.class);
                example2.createCriteria().andEqualTo("userId",askEntity.getUserId());
                List<NjkUserInfoEntity> njkUserInfoEntities = communityUserInfoMapper.selectByExample(example2);
                for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                    historyEntity.setImg(njkUserInfoEntity.getImg());
                }
            }
            ResearchEntity researchEntity = researchMapper.selectByPrimaryKey(historyEntity.getId());
            if (researchEntity!=null){
                Example example1 = new Example(NjkUserEntity.class);
                example1.createCriteria().andEqualTo("userId",researchEntity.getUserId());
                List<NjkUserEntity> njkUserEntities = communityUserMapper.selectByExample(example1);
                for (NjkUserEntity njkUserEntity : njkUserEntities) {
                    historyEntity.setUserName(njkUserEntity.getNickName());
                }
                Example example2 = new Example(NjkUserInfoEntity.class);
                example2.createCriteria().andEqualTo("userId",researchEntity.getUserId());
                List<NjkUserInfoEntity> njkUserInfoEntities = communityUserInfoMapper.selectByExample(example2);
                for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                    historyEntity.setImg(njkUserInfoEntity.getImg());
                }
            }
        }
        PageInfo pageInfo = new PageInfo<>(historyEntities);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     * 研究院查询
     */
    @Transactional
    public ResponseResult SearchResearch(String keyWord, Integer pageNum, Integer pageSize)throws Exception {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(ResearchEntity.class);
        example.createCriteria().andLike("subject","%"+ keyWord+"%");
        List<ResearchEntity> researchEntities = researchMapper.selectByExample(example);
         PageInfo pageInfo = new PageInfo<>(researchEntities);
        return new ResponseResult(ResponseMessage.OK,pageInfo);
    }

    /**
     * 截取内容的前20文字作为简介
     *
     * @param postAllModel
     * @param researchEntity
     * @return
     * @throws Exception
     */
    private List<ResearchImgEntity> getResearchIntro(PostAllModel postAllModel, ResearchEntity researchEntity) throws Exception {
        List<ResearchImgEntity> listImg;
        String str = DateUtil.delHTMLTag(researchEntity.getContent());
        String replace = str.replace("\r", "");
        String replace1 = replace.replace("\n", "");
        String replace2 = replace1.replace("&nbsp;", "");
        //截取前20个文字
        int num = 20;
        if (replace2.length() < 20) {
            postAllModel.setIntro(replace2);
        } else {
            str = replace1.substring(0, num);
            postAllModel.setIntro(str + "...");
        }
        listImg = researchImgMapper.showResearchActivityImg(postAllModel.getId());
        return listImg;
    }

    /**
     * 用户定时清除每日积分信息
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void UserDaySign() {
        // 查询用户签到表
        List<UserTaskEntity> userTaskEntities = userTaskMapperr.selectAll();
        for (UserTaskEntity userTaskEntity : userTaskEntities) {
            if (userTaskEntity.getQuizStatus()==0){
                userTaskEntity.setQuizNum(0);
            }
            if (userTaskEntity.getResearchStatus()==0){
                userTaskEntity.setResearchNum(0);
            }
            if (userTaskEntity.getShareStatus()==0){
                userTaskEntity.setShareNum(0);
            }
            userTaskMapperr.updateByPrimaryKeySelective(userTaskEntity);
        }
    }
}
